﻿using System;
using Framework.MVC;
using Log.Model;
using Identity.Model;
using Account.Model;
using Identity.IService.Entry;
using Account.IService.Entry;
using Framework.MVVM.Model;
using Common.IService;
using Common.Service;
using Log.Service;
using Log.IService;
using Identity.IService;
using Identity.Service;
using Account.Service;
using Account.IService;
using Account.Model.Behavior;

namespace FrontCenter.Models
{
    [ServiceAttribute(typeof(IAccountService), typeof(AccountService), "Instance")]
    [ServiceAttribute(typeof(IIdentityService), typeof(IdentityService), "Instance")]
    [ServiceAttribute(typeof(ILogService), typeof(LogService), "Instance")]
    [ServiceAttribute(typeof(ICodeService), typeof(CodeService), "Instance")]
    public class GetAccountModel : AsyncModelBase, IWriteLog, IValidateIdentity, IGetAccountByCode
    {
        #region Field

        private const string m_resource_user = "u000";
        private const char m_split = ';';

        private const string m_empty_identity = "getacnt00";
        private const string m_error_identity = "getacnt01";
        private const string m_complete_identity = "getacnt02";
        private const string m_empty_account = "getacnt03";
        private const string m_error_account = "getacnt04";
        private const string m_complete_account = "getacnt05";

        private const string m_name = "name";
        private const string m_code = "code";

        #endregion Field

        #region AsyncModelBase

        protected override void OnInit(Type type)
        {
            OnBeginRequest();
            this.ValidateIdentity(m_resource_user, m_empty_identity, m_error_identity, m_complete_identity);
        }

        #endregion AsyncModelBase

        #region Write Log

        public void OnWriteLogComplete(string eventCode)
        {
            OnEndRequest();
        }

        public void OnWriteLogError(string eventCode)
        {
            OnEndRequest();
        }

        #endregion Write Log

        #region IValidateIdentity

        public string IdentityCode { get; set; }
        public string Source { get; set; }
        public bool IsOnce { get; set; }

        public void OnValidateIdentityComplete(string key, IdentityEntry entry)
        {
            OnBeginRequest();
            this.Log(key, entry);

            AccountCode = entry.value;
            this.GetAccount(AccountResource, m_error_account, m_complete_account);
        }

        public void OnValidateIdentityError(string key)
        {
            this.Log(key, new IdentityEntry { code = IdentityCode, source = Source, resource = m_resource_user });
        }

        #endregion IValidateIdentity

        #region IGetAccountByCode

        public string AccountCode { get; set; }

        public string AccountResource { get; set; }

        public void OnGetAccountByCodeComplete(string key, string name)
        {
            OnComplete(m_name, name);
            OnComplete(m_code, IdentityCode);
            this.Log(key, name);
        }

        public void OnGetAccountByCodeError(string key)
        {
            this.Log(key, Source + m_split + AccountCode);
        }

        #endregion IGetAccountByCode
    }
}